// models/wordset.js
const connection = require('../database');

const WordSet = {
    create: (wordsetData, callback) => {
        const query = `
      INSERT INTO wordsets (name, img, wordlist, user_id)
      VALUES (?, ?, ?, ?)
    `;
        connection.query(
            query,
            [
                wordsetData.name,
                wordsetData.img,
                JSON.stringify(wordsetData.wordlist || []),
                wordsetData.user_id,
            ],
            callback
        );
    },
    findOne: (wordsetId, callback) => {
        const query = `
      SELECT * FROM wordsets
      WHERE id = ?
    `;
        connection.query(query, [wordsetId], callback);
    },
    update: (wordsetData, callback) => {
        const query = `
            UPDATE wordsets
            SET name = ?, img = ?, wordlist = ?
            WHERE id = ? AND user_id = ?
            `;
        connection.query(
            query,
            [
                wordsetData.name,
                wordsetData.img,
                JSON.stringify(wordsetData.wordlist || []),
                wordsetData.id,
                wordsetData.user_id,
            ],
            callback
        );
    },
    delete: (wordsetId, userId, callback) => {
        const query = `
      DELETE FROM wordsets
      WHERE id = ? AND user_id = ?
    `;
        connection.query(query, [wordsetId, userId], callback);
    },
    findAll: (where,page, limit, callback) => {
        // page是当前页码，limit是每页显示的数量
        // 计算OFFSET值
        const offset = (page - 1) * limit;
        const query = `
        SELECT * FROM wordsets
        WHERE user_id = ?
        LIMIT ?
        OFFSET ?
    `;
        connection.query(query, [where,parseInt(limit), parseInt(offset)], callback);
    },
    // 其他可能的操作...
};

module.exports = WordSet;